// 购物车数据action
import { get, post, put, del } from '@common/ajax';
import logger from '@common/logger';
import urls from '@common/url';
import {
    MATCH_SHOP_ADDRESS_INFO,
    SEARCH_SHOP_ADDRESS_LIST,
    GET_SHOP_MY_ADDRESS_LIST,
    UPDATE_SHOP_SHOW_ADDRESS_INFO,
    SET_SHOP_EDIT_ADDRESS_INFO,
} from '../mutationType';

const shopAddress = {
    state: {
        addressTip: '',
        addressInfo: null,
        addressList: [],
        searchList: [],
        editAddressInfo: null,
    },
    mutations: {
        MATCH_SHOP_ADDRESS_INFO: (state, addressInfo) => {
            Object.assign(state, { addressInfo, addressTip: addressInfo.address });
        },
        SEARCH_SHOP_ADDRESS_LIST: (state, searchList) => {
            Object.assign(state, { searchList });
        },
        GET_SHOP_MY_ADDRESS_LIST: (state, addressList) => {
            Object.assign(state, { addressList });
        },
        UPDATE_SHOP_SHOW_ADDRESS_INFO: (state, info) => {
            Object.assign(state, {
                addressInfo: info,
                addressTip: info.address,
            });
        },
        SET_SHOP_EDIT_ADDRESS_INFO: (state, editAddressInfo) => {
            Object.assign(state, { editAddressInfo });
        },
    },
    actions: {
        // 获取我的收货地址
        getShopAddressList: async ({ commit }) => {
            const res = await get(urls.address);
            logger.info('ShopAddress => 获取的地址列表为', res.valid);
            commit(GET_SHOP_MY_ADDRESS_LIST, [...res.valid]);
        },
        // 匹配地址相应仓库信息
        matchShopAddress: async ({ commit }, param) => {
            logger.warn('匹配地址信息');
            const info = await get(urls.addressMatch, param);
            commit(MATCH_SHOP_ADDRESS_INFO, info);
        },
        // 获取地址列表-搜索
        searchShopAddressList: async ({ commit }, param) => {
            const res = await get(urls.addressSearch, param);
            logger.success('ShopAddress => 获取的数据为', res.data);
            commit(SEARCH_SHOP_ADDRESS_LIST, res.data);
        },
        // 设置位置信息
        setShopAddressInfo: ({ commit }, info) => {
            commit(UPDATE_SHOP_SHOW_ADDRESS_INFO, info);
        },
        // // 设置编辑的地址信息
        // setEditAddressInfo: ({ commit }, addressInfo) => {
        //     commit(SET_SHOP_EDIT_ADDRESS_INFO, addressInfo);
        // },
        // 添加收货地址
        addShopAddress: async ({ dispatch }, item) => {
            const res = await post(urls.address, item);
            dispatch('getShopAddressList');
            return res ? true : false;
        },
        // 修改收货地址
        editShopAddress: async ({ dispatch }, item) => {
            const res = await put(`${urls.address}/${item.id}`, item);
            dispatch('getShopAddressList');
            return res ? true : false;
        },
        // 删除收货地址
        delShopAddress: async ({ dispatch }, id) => {
            const res = await del(`${urls.address}/${id}`);
            dispatch('getShopAddressList');
            return res ? true : false;
        },
    },
};

export default shopAddress;
